package com.scalar.chiptrack.operations.reports.actions;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.utils.ExcelUtils;
import com.scalar.chiptrack.utils.DateUtil;
import com.scalar.chiptrack.operations.reports.DataDumpReportsHelper;
import com.scalar.chiptrack.operations.reports.forms.ProductDetailsReportForm;
import com.scalar.chiptrack.operations.process.dao.NewProductDAO;
import com.scalar.chiptrack.operations.vendor.dao.VendorDAO;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.commons.Constants;
import com.scalar.chiptrack.commons.ExcelColumnProperties;
import com.scalar.chiptrack.users.User;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.poi.ss.usermodel.Workbook;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: scalar soft
 * Date: Mar 23, 2007
 * Time: 6:12:56 PM
 * To change this template use File | Settings | File Templates.
 */
public class ProductDetailsReportAction extends ChipTrackAction
{
     private static final String ACTION_NAME = "/productDetailsReport.do";
    public ActionForward execute( ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response )
            throws Exception
    {

        String action = request.getParameter( "action" );
        HttpSession session = request.getSession();

        User user = ( User ) session.getAttribute( SessionKeys.USER_KEY );

        if( user == null )
        {
            session.setAttribute( SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME + "?" + request.getQueryString() );
            return ( mapping.findForward( "login" ) );
        }

        ProductDetailsReportForm productDetailsReportForm = ( ProductDetailsReportForm ) form;
        ArrayList mktProductList   = null;

        ArrayList productDetailsList = null;
        Workbook workbook = null;
        String fileName = "";

        if ( action == null )
        {
            action = "";
        }
        try
        {
            if ( "productDetailsReport".equalsIgnoreCase(action) || "".equals(action) )
            {
                request.setAttribute( "mktProductList", NewProductDAO.loadMarketingPartNumbers( null, Constants.LOCATION_TYPE_FG) );
            }
            else if ( "productionReport".equalsIgnoreCase(action))
            {
               String psType = productDetailsReportForm.getPsType();
               try
                {
                    request.setAttribute( "processList", NewProductDAO.loadWipProcesses(null) );
                    if ( "0".equalsIgnoreCase(psType) || psType == null || "".equalsIgnoreCase(psType) )
                    {
                        request.setAttribute( "vendorList", VendorDAO.getVendorNamesByProcessByUserType( null, "all", user ) );
                    }
                    else
                    {
                        request.setAttribute( "vendorList", VendorDAO.getVendorNamesByProcessByUserType( null, psType, user ) );
                    }
                }
                catch( Exception e )
                {
                    e.printStackTrace();
                }

            }
            else
            {
                if ( "generateProductXls".equalsIgnoreCase(action) )
                {
                    String partNumber    = request.getParameter( "prompt0" );
                    String packageType = request.getParameter( "prompt1" );
                    String pinCount   = request.getParameter( "prompt2" );
                    String productGroup   = request.getParameter( "prompt3" );
                    String defaultPrompt= request.getParameter( "prompt4" );
                    
                    ExcelColumnProperties colProps1 = new ExcelColumnProperties("Standard Wafer Cost");
                    colProps1.setCellStyle(ExcelColumnProperties.CELL_STYLE_CURRENCY_$);
                    ExcelColumnProperties colProps2 = new ExcelColumnProperties("Sort Cost");
                    colProps2.setCellStyle(ExcelColumnProperties.CELL_STYLE_CURRENCY_$);
                    ExcelColumnProperties colProps3 = new ExcelColumnProperties("Assy Cost");
                    colProps3.setCellStyle(ExcelColumnProperties.CELL_STYLE_CURRENCY_$);
                    ExcelColumnProperties colProps4 = new ExcelColumnProperties("Test Cost");
                    colProps4.setCellStyle(ExcelColumnProperties.CELL_STYLE_CURRENCY_$);
                    ExcelColumnProperties colProps5 = new ExcelColumnProperties("EOL Cost");
                    colProps5.setCellStyle(ExcelColumnProperties.CELL_STYLE_CURRENCY_$);
                    
                    /*ExcelColumnProperties colProps6 = new ExcelColumnProperties("Vendor Wafer Cost");
                    colProps6.setCellStyle(ExcelColumnProperties.CELL_STYLE_CURRENCY_$);
                    ExcelColumnProperties colProps7 = new ExcelColumnProperties("Vendor Sort Cost");
                    colProps7.setCellStyle(ExcelColumnProperties.CELL_STYLE_CURRENCY_$);
                    ExcelColumnProperties colProps8 = new ExcelColumnProperties("Vendor Assy Cost");
                    colProps8.setCellStyle(ExcelColumnProperties.CELL_STYLE_CURRENCY_$);
                    ExcelColumnProperties colProps9 = new ExcelColumnProperties("Vendor Test Cost");
                    colProps9.setCellStyle(ExcelColumnProperties.CELL_STYLE_CURRENCY_$);
                    ExcelColumnProperties colProps10 = new ExcelColumnProperties("Vendor EOL Cost");
                    colProps10.setCellStyle(ExcelColumnProperties.CELL_STYLE_CURRENCY_$); */

                    ExcelColumnProperties colProps11 = new ExcelColumnProperties("Sort Yield");
                    colProps11.setCellStyle(ExcelColumnProperties.CELL_STYLE_FLOAT_PER);
                    ExcelColumnProperties colProps12 = new ExcelColumnProperties("Assy Yield");
                    colProps12.setCellStyle(ExcelColumnProperties.CELL_STYLE_FLOAT_PER);
                    ExcelColumnProperties colProps13 = new ExcelColumnProperties("Test Yield");
                    colProps13.setCellStyle(ExcelColumnProperties.CELL_STYLE_FLOAT_PER);
                    ExcelColumnProperties colProps14 = new ExcelColumnProperties("Final Yield");
                    colProps14.setCellStyle(ExcelColumnProperties.CELL_STYLE_FLOAT_PER);

                    List<ExcelColumnProperties> list = new ArrayList<ExcelColumnProperties>();

                    list.add(colProps1);  list.add(colProps2); list.add(colProps3);

                    list.add(colProps4); list.add(colProps5); /*list.add(colProps6); list.add(colProps7);
                    list.add(colProps8); list.add(colProps9); list.add(colProps10);*/ list.add(colProps11);
                    list.add(colProps12); list.add(colProps13); list.add(colProps14);

                    productDetailsList = NewProductDAO.loadProductDetails( null,partNumber,packageType,pinCount,productGroup,defaultPrompt);

                    workbook =  DataDumpReportsHelper.getWorkbook( productDetailsList, "Product Details Report", null, null, list );
                    fileName = "ProductDetailsReport_" +DateUtil.getTimeStamp() ;
                }
                ExcelUtils.generateExcelReport( workbook, fileName, response );
            }
        }
        catch ( Exception e)
        {
            //errors.put ( "Could not generate excel file", null );
            e.printStackTrace();
        }

        if( "productDetailsReport".equalsIgnoreCase(action) )
        {
            return ( mapping.findForward( "productDetailsReport" ) );
        }
        else if( "productionReport".equalsIgnoreCase(action) )
        {
            return ( mapping.findForward( "productionReport" ) );
        }
        else
        {
            return null;
        }
    }
}
